package com.beerq.view.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.beerq.R;
import com.beerq.model.UserInfo;
import com.beerq.util.BasicHttp;
import com.beerq.util.BeerQPref;
import com.beerq.util.Constants;
import com.beerq.util.StringUtil;
import com.beerq.view.BaseActivity;
import com.beerq.view.MainActivity;
import com.beerq.view.ProductJsManager;
import com.beerq.view_manager.RegisterAndLoginManager;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import static com.beerq.util.Constants.userInfo;

public class LoginActivity extends BaseActivity {


    private Button btn_Login;

    private EditText mEditTextPhoneNo;
    private EditText mEditTextPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarType = 0;
        super.onCreate(savedInstanceState);
        hideTopBar();
        setMainContentView(R.layout.activity_login);



        initView();

    }

    private void initView() {
        mEditTextPhoneNo = (EditText) findViewById(R.id.edit_phone_number);
        btn_Login = (Button) findViewById(R.id.btn_login);
        mEditTextPassword = (EditText) findViewById(R.id.edit_password);

        btn_Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String phoneNo = mEditTextPhoneNo.getText().toString();
                String password = mEditTextPassword.getText().toString();
                if (StringUtil.isEmpty(phoneNo)) {
                    showToast("手机号码不能为空");
                    return;
                } else if (phoneNo.length() != 11) {
                    showToast("手机号码格式不正确");
                    return;
                } else if (StringUtil.isEmpty(password)) {
                    showToast("密码不能为空");
                    return;
                }

                RegisterAndLoginManager.login(mContext, phoneNo, password,new BasicHttp.IMyCallBack(){
                    @Override
                    public void onResponse(String resonseData) {
                        Intent intent=new Intent(mContext, MainActivity.class);
                        startActivity(intent);
                        JSONObject jsonObject= JSON.parseObject(resonseData);
                        userInfo=UserInfo.parseUserinfo(jsonObject.getJSONObject("Result").toJSONString());
                        String str=JSONObject.toJSONString(userInfo);
                        BeerQPref.getInstance(mContext).setUserInfo(str);




                        ProductJsManager.setUserInfo(Constants.firstWebView, userInfo.getAppUserId());

                        finish();
                    }

                    @Override
                    public void onFailure(String resonseData) {
                        mEditTextPassword.setText("");
                    }
                });

            }
        });

        findViewById(R.id.txt_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, RegisterActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.txt_forget_password).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ModifyPasswordActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.img_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wechat_sdk_微信登录，分享demo_test";
                Constants.getWXAPI(mContext).sendReq(req);

            }
        });


    }

}
